package com.voice.call.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.service.TaskIntegralService;
import com.voice.call.utils.IpUtil;
import com.voice.call.utils.JSONUtil;
import com.voice.call.version.ApiVersion;

//签到管理
@RestController
@RequestMapping("/api/{version}/checkin/")
public class CheckInController {
	
	private final Logger log = LoggerFactory.getLogger(CheckInController.class);
	
	@Resource
	private TaskIntegralService taskIntegralService;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result checkinList(@RequestBody Map<String, Object> params) {
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		return taskIntegralService.getCheckInList(user_id,lang);
	}
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "checked", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result checkinChecked(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		String ip = IpUtil.getIpAddr(request);
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		Result rs = taskIntegralService.userCheckIn(user_id,ip);
		rs.changeLange(lang);
		return rs;
	}

}
